# -*- coding: utf-8 -*-
"""
2024-06-11 F 表格编辑
"""
import os


from PySide6.QtCore import Slot, QSize
from PySide6.QtGui import QAction, QKeySequence
from PySide6.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QHBoxLayout, QGridLayout, QPushButton, QWidget, QMessageBox,QDialog,QLabel, QComboBox, QTableView,QRadioButton,QPlainTextEdit       ## , QStandardItemModel, QStandardItem, QHeaderView, QTableWidget, QTableWidgetItem, QAbstractItemView, QTableView, QTableWidgetItem, QTableWidgetSelectionRange, QTableWidgetItem, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidget
from PySide6.QtCore import Qt
from PySide6.QtGui import QGuiApplication, QCloseEvent
from PySide6.QtWidgets import QMessageBox
## from PySide6.QtCore.Qt import 

##from PySide6.QtCore import WindowState

##from top.wardyan.utils import winformutils
from top.wardyan.core.action.baseaction import WMainWindow,WQdialog
##from top.wardyan.core.widges.basetable import WBaseTableView
from top.wardyan.core.vo.vos import Record, TableCfg, TableColCfg
from top.wardyan.core.utils.tableutil import readTableCfg, saveTableCfg,getTableColCfg,saveTableColCfg
from top.wardyan.core.widges.controlls import *
from top.wardyan.core.vo.const import *






LBLWIDTH = 100
TXTWIDTH = 500


class TableColEditor(WQdialog):


    ## 


    def __init__(self, widgetname : str, tablename : str, field : str) :
        """
        colID: yt_table_col.id
        """
        WQdialog.__init__(self, widgetname)
        self.widgetname : str = None
        self.tablename : str = None
        self.field : str = None
        self.colcfg = None
        self.execresult = 0
        ##
        self.widgetname = widgetname
        ## self.colID = colID
        if tablename is None or field is None:
            QMessageBox.warning(self, "警告", "参数传递错误", QMessageBox.StandardButton.Ok)
            self.close()
        self.tablename = tablename
        self.field = field

        ## else
        # 设置窗口标志以显示最大化和最小化按钮
        self.setWindowFlags(self.windowFlags() | Qt.WindowMinMaxButtonsHint)
        ## 显示窗体具体信息
        self.main()
        ## self.setCentralWidget(WTableEditor())
        
        self.refreshData()

    def on_btnSubmit_clicked(self):
        # 在这里编写当按钮被点击时需要执行的代码
        # 你可以在这里收集表单数据，并进行保存操作等
        if self.colcfg is None :
            QMessageBox.warning(self, "警告", "数据不存在", QMessageBox.StandardButton.Ok)
        else :
            idx = int(self.txtIdx.text())
            
            ## TODO
            self.colcfg.idx = int(self.txtIdx.text())
            self.colcfg.field = self.txtField.text()
            self.colcfg.title = self.txtTitle.text()
            self.colcfg.align = int(self.btngrpAlign.getSelected()[1])
            self.colcfg.width = int(self.txtWidth.text())
            self.colcfg.show = self.chkSHow.isChecked()
            self.colcfg.datatype = int(self.btngrpDatatype.getSelected()[1])
            self.colcfg.format = self.txtFormat.text()
            self.colcfg.replace = self.txtReplace.toPlainText()
            ## self.colcfg.sql = self.txtSQL.toPlainText()
            self.colcfg.sql = self.txtSQL.toPlainText()

            saveTableColCfg(self.colcfg)
            QMessageBox.information(self, "保存结果", "保存成功", QMessageBox.StandardButton.Ok)
            self.execresult = 1
            self.close()




    def main(self) :
        global LBLWIDTH
        global TXTWIDTH


        HEIGHT = 30
        layout : QVBoxLayout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.setLayout(layout)

        wgtField : QWidget = QWidget()
#         wgtField.setStyleSheet("""
#     QWidget {
#         border: 2px solid black; /* 设置边框宽度、样式和颜色 */
#         border-radius: 5px; /* 如果需要圆角，可以设置这个属性 */
#     }
# """)
        layout.addWidget(wgtField)
        lytGrid : QGridLayout = QGridLayout()
        ## lytGrid.setcolumn
        lytGrid.setColumnStretch(0, 1)
        lytGrid.setColumnStretch(1, 99)
        wgtField.setLayout(lytGrid)

        ## 序号 字段名 数据类型 数据类型 标题 是否显示 列宽 对齐方式 格式化 替换字典 SQL
        ##
        lblIdx : QLabel  = QLabel("序号")
        lblIdx.setMinimumHeight(HEIGHT)
        lblIdx.setFixedWidth(LBLWIDTH)
        lblIdx.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(lblIdx,0,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        self.txtIdx : WIntegerLineEdit = WIntegerLineEdit()
        self.txtIdx.setMinimumHeight(HEIGHT)
        self.txtIdx.setFixedWidth(TXTWIDTH)
        lytGrid.addWidget(self.txtIdx,0,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        ##
        lblField : QLabel  = QLabel("字段名")
        lblField.setMinimumHeight(HEIGHT)
        lblField.setFixedWidth(LBLWIDTH)
        lblField.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(lblField,1,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        self.txtField : WLineEdit = WLineEdit()
        self.txtField.setMinimumHeight(HEIGHT)
        self.txtField.setFixedWidth(TXTWIDTH)
        lytGrid.addWidget(self.txtField,1,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)


        lblTitle : QLabel  = QLabel("标题")
        lblTitle.setMinimumHeight(HEIGHT)
        lblTitle.setFixedWidth(LBLWIDTH)
        lblTitle.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(lblTitle,2,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        self.txtTitle : WLineEdit = WLineEdit()
        self.txtTitle.setMinimumHeight(HEIGHT)
        self.txtTitle.setFixedWidth(TXTWIDTH)
        lytGrid.addWidget(self.txtTitle,2,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        ##
        lblDatatype : QLabel  = QLabel("数据类型")
        lblDatatype.setMinimumHeight(HEIGHT)
        lblDatatype.setFixedWidth(LBLWIDTH)
        lblDatatype.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(lblDatatype,3,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        self.btngrpDatatype : WRadioButtonGroup = WRadioButtonGroup()
        self.btngrpDatatype.setHeight(HEIGHT)
        self.btngrpDatatype.setWidth(TXTWIDTH)
        self.btngrpDatatype.setItem(CONST_DATATYPE_NAME)
        lytGrid.addWidget(self.btngrpDatatype,3,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        ##
        lblShow : QLabel  = QLabel("是否显示")
        lblShow.setMinimumHeight(HEIGHT)
        lblShow.setFixedWidth(LBLWIDTH)
        lblShow.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(lblShow,4,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        self.chkSHow : WCheckBox = WCheckBox("显示                         ")
        self.chkSHow.setMinimumHeight(HEIGHT)
        self.chkSHow.setFixedWidth(TXTWIDTH)
        lytGrid.addWidget(self.chkSHow,4,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)


        ##
        lblWidth : QLabel  = QLabel("列宽")
        lblWidth.setMinimumHeight(HEIGHT)
        lblWidth.setFixedWidth(LBLWIDTH)
        lblWidth.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(lblWidth,5,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        self.txtWidth : WIntegerLineEdit = WIntegerLineEdit()
        self.txtWidth.setMinimumHeight(HEIGHT)
        self.txtWidth.setFixedWidth(TXTWIDTH)
        lytGrid.addWidget(self.txtWidth,5,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        
        ##
        lblAlign : QLabel  = QLabel("对齐方式")
        lblAlign.setMinimumHeight(HEIGHT)
        lblAlign.setFixedWidth(LBLWIDTH)
        lblAlign.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(lblAlign,6,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        self.btngrpAlign : WRadioButtonGroup = WRadioButtonGroup()
        self.btngrpAlign.setHeight(HEIGHT)
        self.btngrpAlign.setWidth(TXTWIDTH)
        self.btngrpAlign.setItem(CONST_ALIGN_NAME)
        lytGrid.addWidget(self.btngrpAlign,6,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        ##
        lblFormat : QLabel  = QLabel("格式化")
        lblFormat.setMinimumHeight(HEIGHT)
        lblFormat.setFixedWidth(LBLWIDTH)
        lblFormat.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(lblFormat,7,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        self.txtFormat : WLineEdit = WLineEdit()
        self.txtFormat.setMinimumHeight(HEIGHT)
        self.txtFormat.setFixedWidth(TXTWIDTH)
        lytGrid.addWidget(self.txtFormat,7,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        ## 
        lblReplace : QLabel  = QLabel("替换字典")
        lblReplace.setMinimumHeight(HEIGHT)
        lblReplace.setFixedWidth(LBLWIDTH)
        lblReplace.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(lblReplace,8,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        self.txtReplace : WPlainTextEdit = WPlainTextEdit()
        self.txtReplace.setMinimumHeight(HEIGHT * 3)
        self.txtReplace.setFixedWidth(TXTWIDTH)
        lytGrid.addWidget(self.txtReplace,8,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        ##
        lblSQL : QLabel  = QLabel("SQL")
        lblSQL.setMinimumHeight(HEIGHT)
        lblSQL.setFixedWidth(LBLWIDTH)
        lblSQL.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(lblSQL,9,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        self.txtSQL : WPlainTextEdit = WPlainTextEdit()
        self.txtSQL.setMinimumHeight(HEIGHT * 5)
        self.txtSQL.setFixedWidth(TXTWIDTH)
        lytGrid.addWidget(self.txtSQL,9,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        ##
        btnSubmit : QPushButton = QPushButton("保存")
        btnSubmit.setMinimumHeight(50)
        ## btn.clicked.connect(button_click_script)
        btnSubmit.setFixedWidth(LBLWIDTH)
        ## 添加保存按钮的点击事件连接
        btnSubmit.clicked.connect(self.on_btnSubmit_clicked)

        lytGrid.addWidget(btnSubmit,10,1, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)

        

        ##
        lytGrid.activate()

    def refreshData(self) :
        colcfg : TableColCfg = getTableColCfg(self.widgetname, self.tablename, self.field)
        self.colcfg = colcfg
        if colcfg is not None :
            self.tableid = colcfg.tableID
            self.colid = colcfg.id
            self.txtField.setText(colcfg.field)
            ## self.cmbDataType.setText(str(colcfg.datatype))
            ## self.cmbDataType.setCurrentText(CONST_DATATYPE_VALUE[colcfg.datatype])
            self.btngrpDatatype.setChecked(CONST_DATATYPE_VALUE[colcfg.datatype])
            self.txtTitle.setText(colcfg.title)
            ## self.txtShow.setText(str(colcfg.show))
            # if colcfg.show == True :
            #     self.chkSHow.setCheckState(Qt.CheckState.Checked)
            # else :
            #     self.chkSHow.setCheckState(Qt.CheckState.Unchecked)
            self.chkSHow.setChecked(colcfg.show)

            self.txtWidth.setText(str(colcfg.width))
            ## self.txtAlign.setText(str(colcfg.align))
            ##self.cmbAlign.setCurrentText(CONST_ALIGN_VALUE[colcfg.align])
            self.btngrpAlign.setChecked(CONST_ALIGN_VALUE[colcfg.align])
            

            self.txtFormat.setText(colcfg.format)
            self.txtReplace.setPlainText(colcfg.replace)
            self.txtSQL.setPlainText(colcfg.sql)
            self.txtIdx.setText(str(colcfg.idx))

        else :
            QMessageBox.warning(self, "警告", "未找到列设置", QMessageBox.StandardButton.Ok)
            self.close()

    def exec(self) -> int:
        super().exec()
        return self.execresult

if __name__ == "__main__" :
    app = QApplication([])
    window = TableColEditor("tablemanagercoledit")
    window.show()
    app.exec()